//
//  ManageFeeInfoViewController.m
//  wtshoppingcite
//
//  Created by TW on 15-1-29.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "ManageFeeInfoViewController.h"
#import "ManageFeeViewController.h"
#import "BillsModel.h"
#import "ManageFeeModel.h"
#import "RoomModel.h"
#import "ManageBillDetailViewController.h"

@interface ManageFeeInfoViewController ()<UITableViewDelegate,UITableViewDataSource,manageFeeVCdelegate>

@property(nonatomic,strong)UITableView *headTableView;
@property(nonatomic,strong)UITableView *manageFeeTableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)ManageFeeModel *manageFeeModel;
@property(nonatomic,strong)RoomModel *roomModel;

@end

@implementation ManageFeeInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitleView:@"待缴费列表"];
    
    self.view.backgroundColor = UIColorFromRGB(0xfbfbfb);
    
    _dataArray =  [[NSMutableArray alloc]initWithCapacity:1];
    [self getManageFeeBill];
    
    _headTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 5, kWidth, 100) style:UITableViewStylePlain];
    _headTableView.dataSource = self;
    _headTableView.delegate = self;
    _headTableView.scrollEnabled = NO;
    _headTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_headTableView];
    
    
    _manageFeeTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _headTableView.bottom, kWidth, kHeight-_headTableView.height-TAB_BAR_HEIGHT-12) style:UITableViewStylePlain];
    _manageFeeTableView.dataSource = self;
    _manageFeeTableView.delegate = self;
    _manageFeeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_manageFeeTableView];
    
    // Do any additional setup after loading the view.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == _headTableView) {
        return 1;
    }
    if(tableView == _manageFeeTableView){
        
        return _dataArray.count;
    }
    
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"DefaultCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DefaultCell"];
    }
    
    if (tableView == _headTableView) {
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"PropertyCell"];
        
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PropertyCell"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            UILabel *label = [self createLabelWithFrame:CGRectMake(10, 10, kWidth-40, 100) text:@"" font:18 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
            label.tag = 100;
            label.numberOfLines = 0;
            [cell.contentView addSubview:label];
            
            UIView *seperateLine = [[UIView alloc]initWithFrame:CGRectMake(0, 100 - 1, kWidth, 1)];
            seperateLine.backgroundColor = MAIN_GRAY_COLOR;
            [cell.contentView addSubview:seperateLine];
        }
        
        UILabel *label =(UILabel *) [cell.contentView viewWithTag:100];
        label.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"text"];
      
        return cell;

    }
    
    if (tableView == _manageFeeTableView){
        
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"PrestoreCell"];
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PrestoreCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            UILabel *timeLabel = [self createLabelWithFrame:CGRectMake(10, 10, kWidth-40, 20) text:@"" font:14 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
            timeLabel.tag = 1000;
            [cell.contentView addSubview:timeLabel];
            
            
            UILabel *orderLabel = [self createLabelWithFrame:CGRectMake(10, timeLabel.bottom+10, kWidth-40, 20) text:@"" font:14 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
            orderLabel.tag = 1001;
            [cell.contentView addSubview:orderLabel];
            
            UILabel *stateLabel = [self createLabelWithFrame:CGRectMake(10, orderLabel.bottom+10, kWidth-40, 20) text:@"" font:14 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
            stateLabel.tag = 1002;
            [cell.contentView addSubview:stateLabel];
            
            UIView *seperateLine = [[UIView alloc]initWithFrame:CGRectMake(0, 100 - 1, kWidth, 1)];
            seperateLine.backgroundColor = MAIN_GRAY_COLOR;
            [cell.contentView addSubview:seperateLine];
            
        }
        
        BillsModel *billsModel = _dataArray[indexPath.row];
        
        UILabel *timeLabel = (UILabel *)[cell.contentView viewWithTag:1000];
        timeLabel.text = [NSString stringWithFormat:@"账单时间:%@",billsModel.createTime];
        
        UILabel *orderLabel= (UILabel *)[cell.contentView viewWithTag:1001];
        orderLabel.text =[NSString stringWithFormat:@"账单号:%@",billsModel.orderNumber];
        
        UILabel *stateLabel= (UILabel *)[cell.contentView viewWithTag:1002];
        stateLabel.text = [NSString stringWithFormat:@"金额:%@",billsModel.totalCost];
        
        return cell;

        
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == _headTableView) {
        
        ManageFeeViewController *manageFeeVC = [[ManageFeeViewController alloc]init];
        manageFeeVC.hidesBottomBarWhenPushed = YES;
        manageFeeVC.delegate = self;
        [self.navigationController pushViewController:manageFeeVC animated:YES];
    }
    if (tableView == _manageFeeTableView) {
        
        ManageBillDetailViewController *manageBillDetailVC = [[ManageBillDetailViewController alloc]init];
        manageBillDetailVC.hidesBottomBarWhenPushed = YES;
        manageBillDetailVC.billsModel = _dataArray[indexPath.row];
        manageBillDetailVC.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"text"];
        [self.navigationController pushViewController:manageBillDetailVC animated:YES];
    }
    
}


#pragma mark 物业管理代理
-(void)manageAddress:(NSString *)title manageFeeModel:(ManageFeeModel *)manageFeeModel roomModel:(RoomModel *)roomModel{
    
    _manageFeeModel = manageFeeModel;
    _roomModel = roomModel;
    
    NSString *buildId = _manageFeeModel.manageFeeId;
    NSString *roomId = _roomModel.unitId;
    
    self.text = [NSString stringWithFormat:@"%@\n%@",title,[RegisterHelper shareHelper].registerPhoneNum];
    
    [[NSUserDefaults standardUserDefaults] setObject:self.text forKey:@"text"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    [[NSUserDefaults standardUserDefaults] setObject:buildId forKey:@"buildId"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [[NSUserDefaults standardUserDefaults] setObject:roomId forKey:@"roomId"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [_headTableView reloadData];
    [self.navigationController popViewControllerAnimated:YES];
    
    [_dataArray removeAllObjects];
    [self getManageFeeBill];
    
}

#pragma mark 物业费清单
-(void)getManageFeeBill{
    
    NSString *cid = [RegisterHelper shareHelper].citesId;
    NSString *state = @"1";
    NSString *type = @"1";
    
    NSString *buildingId = [[NSUserDefaults standardUserDefaults]objectForKey:@"buildId"];
    NSString *roomId = [[NSUserDefaults standardUserDefaults]objectForKey:@"roomId"];
    
    NSMutableDictionary *listDic = [NSMutableDictionary dictionary];
    SetValueForKey(listDic, cid, @"cid");
    SetValueForKey(listDic, buildingId, @"buildingId");
    SetValueForKey(listDic, roomId, @"roomId");
    SetValueForKey(listDic, state, @"state");
    SetValueForKey(listDic, type, @"type");
    
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kBills
                             andHttpMethod:@"POST"
                             andParameters:listDic
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         
                         request.requestCache = YES;
                         
                         if (response.isError) {
                             
                             DLog(@"error %@",response.errorMessage);
                             
                         }else{
                             
                             NSDictionary *responseDic = request.responseJSON;
                             DLog(@"成功数据:%@",responseDic);
                             
                             NSArray *list = [responseDic objectForKey:@"bills"];
                             
                             for (NSDictionary *dic in list) {
                                 
                                 BillsModel *billsModel = [[BillsModel alloc]initWithBillsModelList:dic];
                                 [self.dataArray addObject:billsModel];
                             }
                         }
                         
                         [_manageFeeTableView reloadData];
                     }];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
